package com.yuexun.dhdoctor.module.Contract.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.bean.GroupDataBean;
import com.yuexun.dhdoctor.module.Contract.bean.UsersBean;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenss on 2018/1/19 0019.
 */

public class DepartmentListAdapter extends BaseAdapter {
    boolean isDepart = false;
    private Context context;
    Handler handler;
    protected LayoutInflater mInflater;
    private List<GroupDataBean> mDatas = new ArrayList<>();
    private List<GroupDataBean> selectValues = new ArrayList<>();
    private List<GroupDataBean> mOriginalValues;

    //0：不能选择自己  1：能选择自己
    private int isSelectMe;

    public DepartmentListAdapter(Context context, List<GroupDataBean> mDatas,
                                 ArrayList<GroupDataBean> selectValues, Handler handler, boolean isDepart, int isSelectMe) {
        this.context = context;
        this.isDepart = isDepart;
        this.mDatas = mDatas;
        this.handler = handler;
        this.selectValues = selectValues;
        this.isSelectMe = isSelectMe;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public DepartmentListAdapter(Context context, List<GroupDataBean> mDatas, int isSelectMe) {
        this.context = context;
        this.mDatas = mDatas;
        this.isSelectMe = isSelectMe;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setmDatas(List<GroupDataBean> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mDatas == null) {
            return 0;
        }
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final viewHolder1 holder1;
        if (view == null) {
            view = this.mInflater.inflate(R.layout.item_department, null);
            holder1 = new viewHolder1();
            holder1.name = (TextView) view.findViewById(R.id.item_department_name);
            holder1.count = (TextView) view.findViewById(R.id.item_department_count);
            holder1.checkBox = (CheckBox) view.findViewById(R.id.add_checkbox);
            view.setTag(holder1);
        } else {
            holder1 = (viewHolder1) view.getTag();
        }

        holder1.name.setText(mDatas.get(i).getDepartment_name());
        holder1.count.setText(mDatas.get(i).getBmrs() + "人");
        if (isDepart) {
            holder1.checkBox.setVisibility(View.VISIBLE);
            if (mDatas.get(i).getUsers() != null || mDatas.get(i).getChildDepartments() != null) {
                List<UsersBean> usersBeen = mDatas.get(i).getUsers();
                List<GroupDataBean> childDepartmentsBeen = mDatas.get(i).getChildDepartments();
                if ((usersBeen != null && usersBeen.size() > 0) || (childDepartmentsBeen != null && childDepartmentsBeen.size() > 0)) {
                    if (isSelectMe == 0) {
                        if (PreferenceHelper.readString(context, AppConfig.USER_INFO, AppConfig.DEPARTMENTID)
                                .equals(mDatas.get(i).getDepartment_id() + "")) {
                            holder1.checkBox.setBackgroundResource(R.drawable.add_member_unselect);
                            holder1.checkBox.setEnabled(false);
                        } else {
                            holder1.checkBox.setBackgroundResource(R.drawable.check_addmember_blue);
                            holder1.checkBox.setEnabled(true);
                        }
                    } else {
                        holder1.checkBox.setBackgroundResource(R.drawable.check_addmember_blue);
                        holder1.checkBox.setEnabled(true);
                    }
                } else {
                    holder1.checkBox.setBackgroundResource(R.drawable.add_member_unselect);
                    holder1.checkBox.setEnabled(false);
                }
            } else {
                holder1.checkBox.setBackgroundResource(R.drawable.add_member_unselect);
                holder1.checkBox.setEnabled(false);
            }
        } else {
            holder1.checkBox.setVisibility(View.GONE);
            holder1.checkBox.setEnabled(false);
        }

        holder1.checkBox.setChecked(false);

        for (GroupDataBean bean : selectValues) {
            if (bean.getDepartment_id() == mDatas.get(i).getDepartment_id()) {
                holder1.checkBox.setChecked(true);
            }
        }

        holder1.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!((CheckBox) view).isChecked()) {
                    selectValues.remove(mDatas.get(i));
                    holder1.checkBox.setChecked(false);
//                    notifyDataSetChanged();
                    Message msg = new Message();
                    msg.what = 10003;
                    handler.sendMessage(msg);
                } else {
                    selectValues.clear();
                    selectValues.add(mDatas.get(i));
                    holder1.checkBox.setChecked(true);
                    notifyDataSetChanged();
                    Message msg = new Message();
                    msg.what = 10003;
                    handler.sendMessage(msg);
                }
            }
        });
        return view;
    }

    class viewHolder1 {
        TextView name;
        TextView count;
        CheckBox checkBox;
    }

    public void getMyFilter(String s, List<GroupDataBean> departmentAll) {
        if (mOriginalValues == null) {
            mOriginalValues = new ArrayList<GroupDataBean>(mDatas);
        }
        if (!s.isEmpty()) {
            ArrayList<GroupDataBean> list = new ArrayList<GroupDataBean>();
            for (int j = 0; j < departmentAll.size(); j++) {
                GroupDataBean data = departmentAll.get(j);
                if ((data.getDepartment_name() != null && data.getDepartment_name().contains(s))
                        || (data.getFirstPinyin() != null && data.getFirstPinyin().contains(s.toString().toUpperCase()))
                        || (data.getFullPinyin() != null && data.getFullPinyin().contains(s.toString().toUpperCase()))) {
                    if (s.equals(data.getDepartment_name()) || s.equals(data.getFirstPinyin()) || s.equals(data.getFullPinyin())) {
                        list.add(0, data);
                    } else {
                        list.add(data);
                    }
                }
            }
            mDatas = list;
        } else {
            mDatas = mOriginalValues;
        }

        notifyDataSetChanged();

    }
}
